package org.openkanban.client.ui.presenter;

import org.openkanban.client.Main;
import org.openkanban.client.ui.board.BoardView;
import org.openkanban.client.util.RootPanel;
import org.openkanban.shared.dto.BoardDto;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.AnimationCallback;

/**
 * Callback that handles switch board request
 */
public class SwitchBoardRequestCallback implements RequestCallback {

    private BoardView boardView;

    public SwitchBoardRequestCallback(BoardView boardView) {
	this.boardView = boardView;
    }

    @Override
    public void onError(Request request, Throwable exception) {

    }

    @Override
    public void onResponseReceived(Request request, final Response response) {
	final BoardDto switchBoardDto = new BoardDto(response.getText());
	if (switchBoardDto.getBoardId() != null) {
	    boardView.animateFade(0, new AnimationCallback() {
		@Override
		public void execute(boolean earlyFinish) {
		    RootPanel.boardComponent.switchBoard(switchBoardDto.getBoardId());
		    boardView.animateFade(100, null, Main.DEFAULT_FADE_DURATION);
		}
	    }, Main.DEFAULT_FADE_DURATION);
	} else {
	    SC.say("No board found.");
	}
    }
}
